package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.ProductDal;
import com.lightdz.tshirtdz.dal.impl.ShoppingCarDal;
import com.lightdz.tshirtdz.model.Enum.SortTypeEnum;
import com.lightdz.tshirtdz.model.PageInfo;
import com.lightdz.tshirtdz.model.api.ApiPStyle;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.api.ApiShoppingCar;
import com.lightdz.tshirtdz.model.entity.ProductEntity;
import com.lightdz.tshirtdz.model.entity.ShoppingCarEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class ShoppingCarBll {

    @Autowired
    ShoppingCarDal shoppingCarDal;
    @Autowired
    ProductDal productDal;

    /**
     * 我的购物车列表
     *
     * @param wxId
     * @param pageSize
     * @param pageNo
     * @param sortType
     * @return
     */
    public ApiResponse<List<ApiShoppingCar>> getMyShoppingCar(String wxId, int pageSize, int pageNo, SortTypeEnum sortType) {
        ApiResponse<List<ApiShoppingCar>> apiResponse = ApiResponse.getNoResponse();

        List<ApiShoppingCar> result = new ArrayList<>();

        try {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageNo(pageNo);
            pageInfo.setPageSize(pageSize);
            pageInfo.setSortType(sortType);

            List<ShoppingCarEntity> carEntityList = shoppingCarDal.getShoppingCarList(wxId, pageInfo);
            if (!carEntityList.isEmpty()) {

                ApiShoppingCar apiCar = new ApiShoppingCar();
                ProductEntity product = null;
                for (ShoppingCarEntity car : carEntityList) {

                    product = productDal.getProduct(car.getPid());
                    if (product != null) {
                        apiCar.setDesc(product.getpDesc());
                        apiCar.setBannerPicUrl(product.getpBannerPicUrl());
                        apiCar.setPrice(product.getpPrice());
                        apiCar.setName(product.getpName());
                        ApiPStyle pStyle = new ApiPStyle();
                        pStyle.setSizeList(Arrays.asList(car.getpSize()));
                        pStyle.setpCount(car.getpCount());
                        pStyle.setColorList(Arrays.asList(car.getpColor()));
                        pStyle.setPid(car.getPid());
                        pStyle.setWxId(wxId);
                        apiCar.setpStyle(pStyle);

                        result.add(apiCar);
                    }

                }
            }
            apiResponse = new ApiResponse<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ShoppingCarBll.getMyShoppingCar", e);
        }
        return apiResponse;
    }

    public ApiResponse<Boolean> addShopingCar(String wxId, String pSize, int pCount, String pColor, long pid) {
        ApiResponse<Boolean> apiResponse = ApiResponse.getNoResponse();

        boolean result = false;

        try {
            //如果存在已存在记录，则变成更新
            ShoppingCarEntity entity = shoppingCarDal.getOneShoppingCar(wxId, pid);
            if (entity != null) {
                result = shoppingCarDal.updateShoppingCar(wxId, pid, pCount + entity.getpCount());
            } else {
                ApiShoppingCar car = new ApiShoppingCar();

                ApiPStyle pStyle = new ApiPStyle();
                pStyle.setSizeList(Arrays.asList(pSize));
                pStyle.setpCount(pCount);
                pStyle.setColorList(Arrays.asList(pColor));
                pStyle.setPid(pid);
                pStyle.setWxId(wxId);
                car.setpStyle(pStyle);

                result = shoppingCarDal.addShoppingCar(wxId, car);
            }
            apiResponse = new ApiResponse<>(result);
        } catch (Exception e) {
            e.printStackTrace();
            LogHelper.error("ShoppingCarBll.getMyShoppingCar", e);
        }
        return apiResponse;
    }

    public ApiResponse<Boolean> deleteShoppingCar(String wxId, long pid) {
        boolean result = shoppingCarDal.deleteShoppingCar(wxId, pid);
        return  new ApiResponse<>(result);
    }

    public ApiResponse<Boolean> updateShoppingCar(String wxId, long pid, int pCount) {
        boolean result = shoppingCarDal.updateShoppingCar(wxId, pid, pCount);
        return  new ApiResponse<>(result);
    }
}
